﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;

namespace Library
{
    public partial class BookTypeViewForm : Form
    {
        public BookType bookType = new BookType();
        bool add = false;
        LibraryEntities1 ins = new LibraryEntities1();
        public static ObjectSet<BookType> bookTypes;
        public BookTypeViewForm(BookType bookType, bool add)
        {
            InitializeComponent();
            this.bookType = bookType;
            this.add = add;
            bookTypes = ins.BookType;
        }

        private void BookTypeViewForm_Load(object sender, EventArgs e)
        {
            if (bookType != null)
            {
                textBox1.Text = bookType.TypeName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (bookType == null)
                bookType = new BookType();
            else
                bookType = bookTypes.Where(x => x.idBookType.Equals(bookType.idBookType)).FirstOrDefault();

            bookType.TypeName = textBox1.Text;
            if (add)
            {
                ins.AddToBookType(bookType);
            }
            ins.SaveChanges();

            this.Close();
        }
    }
}
